package com.zy.settings.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.zy.activity.BaseFragment
import com.zy.net.NetServiceImpl
import com.zy.net.data.DataRecord
import com.zy.settings.R
import com.zy.storage.BaseInfoData
import com.zy.storage.doc.LocalDocInfo
import com.zy.utils.ThreadUtils
import com.zy.zlog.ZLog
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Request
import okhttp3.Response
import java.io.IOException


private const val TAG = "DataBackupHistoryFragment"

class DataBackupHistoryFragment : BaseFragment() {

    private var mAdapter: DataBackupHistoryViewAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_data_backup_history, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val rv: RecyclerView = view.findViewById(R.id.data_backup_history_rv)
        with(rv) {
            layoutManager = LinearLayoutManager(context)
            mAdapter = DataBackupHistoryViewAdapter(parentFragmentManager)
            adapter = mAdapter
        }

        //getAll1()
        //getAllStr()
        getAllDataRecordList()
    }

    private fun getAll() {
        val request: Request = Request.Builder().url(NetServiceImpl.GET_ALL).build()
        NetServiceImpl.INSTANCE.request(request, object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                ZLog.d(TAG, "onFailure:${e}")
            }

            override fun onResponse(call: Call, response: Response) {
                val dataStr = response.body?.string()

                val dataRecord = Gson().fromJson(dataStr, DataRecord::class.java)
                ZLog.d(TAG, "onResponse dataRecord:${dataRecord}")

                //数据处理
                ThreadUtils.mainRun {
                    updateRecordList(dataRecord)
                }
            }
        })
    }

    private fun getAllStr() {
        NetServiceImpl.INSTANCE.getAllStr(callback = object : retrofit2.Callback<String> {
            override fun onResponse(
                call: retrofit2.Call<String>,
                response: retrofit2.Response<String>
            ) {
                val dataStr = response.body()
                val dataRecord = Gson().fromJson(dataStr, DataRecord::class.java)
                ZLog.d(TAG, "onResponse dataRecord:${dataRecord}")

                //数据处理
                ThreadUtils.mainRun {
                    updateRecordList(dataRecord)
                }
            }

            override fun onFailure(call: retrofit2.Call<String>, t: Throwable) {
                ZLog.d(TAG, "onFailure:${t}")
            }
        })
    }

    private fun getAllDataRecordList() {
        NetServiceImpl.INSTANCE.getAll(callback = object : retrofit2.Callback<DataRecord> {
            override fun onResponse(
                call: retrofit2.Call<DataRecord>,
                response: retrofit2.Response<DataRecord>
            ) {
                val dataRecord = response.body()
                ZLog.d(TAG, "onResponse dataRecord:${dataRecord}")

                //数据处理
                ThreadUtils.mainRun {
                    if (dataRecord != null) {
                        updateRecordList(dataRecord)
                    }
                }
            }

            override fun onFailure(call: retrofit2.Call<DataRecord>, t: Throwable) {
                ZLog.d(TAG, "onFailure:${t}")
            }
        })
    }

    fun updateRecordList(dataRecord: DataRecord) {
        mAdapter?.updateData(dataRecord)
        mAdapter?.notifyDataSetChanged()
    }

    companion object {

        @JvmStatic
        fun newInstance() = DataBackupHistoryFragment()
    }
}